#ifndef _SHARED_REFINSTANCE_H_
#define _SHARED_REFINSTANCE_H_

#define RELEASE( ref ) if( ref != NULL ) ref->Release(); ref = NULL;

namespace Hiranipra {

	class RefInstance
	{
	public:
		RefInstance() :
			_counter( 1 )
		{
		}

		virtual ~RefInstance()
		{
		}

		void Retain() throw()
		{
			_counter++;
		}

		uint32 Release()
		{
			if( _counter == 0 )
				throw runtime_error( "Attempt to double release" );
			if( --_counter == 0 )
			{
				delete this;
				return 0;
			}
			return _counter;
		}
		
	private:
		uint32		_counter;
	};

}

#endif // _SHARED_REFINSTANCE_H_
